/*
 * VHMatchdaySelector.java
 * Created on 6 juil. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.List;

import com.extjs.gxt.ui.client.Style.SortDir;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.res.VHResMessages;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.ui.base.VHSelector;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * Select box that allows to select a matchday. The selected matchday can then be used to
 * adjust which standings or matches are displayed.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHMatchdaySelector extends VHSelector implements VHRpcResultReceiver<List<VHJsData>>
{
  /**
   * Creates a new matchday selector.
   */
  public VHMatchdaySelector()
  {
    getCombobox().setDisplayField( "text" );
    getCombobox().setValueField( "num" );
    getCombobox().setWidth( 200 );
  }

  /**
   * Sets the matchdays coming from the server, determines the currently active matchday
   * and provides a text property for all items.
   */
  public void setRpcData( List<VHJsData> listMatchdays, String strPurpose )
  {
    VHResMessages msg = VHMain.getMessages();
    VHJsData mdSelected = null;

    for ( VHJsData md : listMatchdays )
    {
      md.set( "text", msg.matchdayHeader( md.getInt( "moveday_id" ), md.getInt( "num" ) ) );

      if ( mdSelected == null || md.getBoolean( "analysed" ) )
        mdSelected = md;
    }

    if ( mdSelected == null && !listMatchdays.isEmpty() )
      mdSelected = listMatchdays.get( 0 );

    getStore().removeAll();
    getStore().add( listMatchdays );
    getStore().sort( "num", SortDir.ASC );

    // Select active matchday
    //
    if ( !listMatchdays.isEmpty() )
      getCombobox().setValue( mdSelected );

   setInitialised( true );
  }
}
